package com.xbongbong.saas.model;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.InstockProductEntity;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Jian.Wang
 * @version v1.0
 * @date 2019/1/25 10:18
 * @since v1.0
 */
public interface OutstockProductModel extends IModel<OutstockProductEntity> {
    /**
     * 保存数据
     *
     * @param outstockProduct 入库单产品实体数据
     * @return 数据id
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    Integer save(OutstockProductEntity outstockProduct);
    /**
     * 根据主键删除记录
     *
     * @param key    主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    Integer deleteByKey(@Param("key") Long key, @Param("corpid") String corpid);
    /**
     * 删除多条入库单下的所有产品
     *
     * @param corpid    公司ID
     * @param outstockIdIn 入库单Id集合
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    Integer deleteByOutstockIdIn(@Param("corpid") String corpid, @Param("outstockIdIn") List<Long> outstockIdIn);
    /**
     * 删除入库单下的所有产品
     *
     * @param refId    id集合
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    Integer deleteByOutstockId(@Param("refId") Long refId, @Param("corpid") String corpid);
    /**
     * 获取一条入库单产品数据
     *
     * @param key 采购合同产品ID
     * @param corpid 公司id
     * @return OutstockProductEntity 一条数据实体
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    OutstockProductEntity getByKey(@Param("key") Long key, @Param("corpid") String corpid);
    /**
     * 根据出库单idIn获取对应出库单产品
     *
     * @param corpid 公司id
     * @param outstockIdIn 出库单idIn
     * @return List<OutstockProductEntity> 数据集合
     * @author lcx
     * @date 2019/2/21 15:18
     */
    List<OutstockProductEntity> getProductsByOutstockIdIn( String corpid, List<Long> outstockIdIn);

    /**
     * 根据出库单id和type获取对应出库单产品
     *
     * @param corpid 公司id
     * @param outstockIdIn 出库单idIn
     * @param type 类型
     * @return List<OutstockProductEntity> 数据集合
     * @author 吴峰
     * @date 2021/9/24 11:14
     */
    List<OutstockProductEntity> getProductsByOutstockIdAndType( String corpid, List<Long> outstockIdIn, Integer type);

    /**
     * 根据出库单id获取对应出库单产品
     * @param corpid 公司id
     * @param outstockId 出库单id
     * @param delIn
     * @return 关联产品
     * @author 吴峰
     * @date 2021/9/16 15:21
     */
    List<OutstockProductEntity> getProductsByOutstockIdIn(String corpid, Long outstockId, List<Integer> delIn);
    /**
     * 模糊查询
     *
     * @param corpid 参数集合
     * @param nameOrNoLike 模糊查询值
     * @return List<Long> 数据集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    List<Long> findOutstockIdsByRelaProNameOrNo(String corpid, String nameOrNoLike);

    /**
     * 获取产品总销量
     * @param param
     * @return java.lang.Double
     * @author zcp
     * @date 2019/4/16 10:22
     * @since v1.0
     * @version v1.0
     */
    Double getProductNumSum(Map<String ,Object>  param);

    /**
     * 对于可以分批出库的单据，某些场景需要将多次出库的同一个产品数量叠加
     *
     * @param outstockProductList 出库单关联产品
     * @author GuoJun.HU
     * @since 3.19
     * 创建时间 2018/3/15 16:04
     */
    Collection<OutstockProductEntity> mergeOutstockProduct(List<OutstockProductEntity> outstockProductList);

    /**
     * 获取同一仓库下的产品map
     *
     * @param updateWarehouseProducts 产品列表
     * @return 仓库id & 产品Map
     */
    Map<Long, List<OutstockProductEntity>> getOutstockProductMap(List<OutstockProductEntity> updateWarehouseProducts);

    Integer deleteBatchById(Collection<Long> delOutstockProductIdList, String corpid, Integer del);

    void insertBatch(List<OutstockProductEntity> addOutstockProductEntityList);

    void updateOutBatch(List<OutstockProductEntity> updateBatchList, String corpid);

    /**
     * 合同产品排除已出库
     * @param contractProductMap
     * @param corpid
     * @param dataIdList
     */
    void filterOutstockProduct(Map<Long,List<ContractProductEntity>> contractProductMap, String corpid, List<Long> dataIdList) throws XbbException;

    /**
     * 合同销售未出库数量
     * @param contractIdSet
     * @param corpid
     * @return
     */
    Map<Long,Double> contractUnOutstock(Set<Long> contractIdSet, String corpid) throws XbbException;

    List<BusinessProductEntity> findProductNum(Map<String,Object> param);

    Integer findProductNumCount(Map<String,Object> param);

    Integer updateBatch(List<OutstockProductEntity> updateList, String corpid) throws XbbException;

    Long isProductSubAttrExist(String attr, String corpid, Long formId, Integer isArray);

    List<OutstockProductEntity> findForJob();

    /**
     * 出库单上游单据的出库产品
     * @param corpid
     * @param type
     * @param refId
     * @return
     */
    List<OutstockProductEntity> getUpStreamProduct(String corpid, Integer type, Long refId) throws XbbException;

    OutstockProductEntity getProductsByInstockIdAndRefProductId(Long outstockId, Long refProductId, String corpid);

    /**
     * 子表单数字字段汇总
     * @param corpid 公司id
     * @param field 需要汇总的字段
     * @param refId 单据id
     * @param productIdIn 单据在该产品关联的产品id 如生产单有物料产品和成品产品 refId为生产单id productIdIn为物料产品的id
     * @return
     */
    Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn);

    /**
     * 子表单数字字段汇总
     * @param corpid 公司id
     * @param field 需要汇总的字段
     * @param refId 单据id
     * @param productIdIn 单据在该产品关联的产品id 如生产单有物料产品和成品产品 refId为生产单id productIdIn为物料产品的id
     * @return
     */
    Double summaryByAttrContractOutStock(String field,String corpid,  Long refId, List<Long> productIdIn);

    Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn);

    /**
     * Description: 一致性同步
     * @param corpid
     * @param productId
     * @param productName
     * @param specification
     * @param productNo
     * @return java.lang.Integer
     * @author 魏荣杰
     * @date 2019/12/4 3:58 下午
     * @since v4.7.1
     */
    Integer dataConsistencyUpdate(String corpid, Long productId, String productName, String specification, String productNo);

    Long countProductSub(String attr, String corpid, Long formId, Integer isArray);

    List<OutstockProductEntity> getProductWithSubData(String attr, String corpid, Long formId, Integer isArray, Long idGte, Integer limit);

    /**
     * 批量编辑
     * @param list
     * @param corpid
     * @return
     */
    Integer updateMulti(List<OutstockProductEntity> list, String corpid);

    void updateOutstockProductById(List<OutstockProductEntity> outstockProductEntityList, String corpid);

    List<OutstockProductEntity> getOutstockProductById(List<Long> outstockProductId, String corpid);

    List<OutstockProductEntity> getOutstockProductByRefId(List<Long> refId, String corpid);
    /**
     * 获取工单出库数据
     * @param list
     * @param corpid
     * @return
     */
    List<OutstockProductEntity> getOrderOutStockData(Long dataId, String corpid);

    OutstockProductEntity getOutstockProductSum(Map<String, Object> map);

    Set<Long> getWarehouseIdUnion(Set<Long> warehouseIdList, String corpid);
}
